package com.zhaoyue.study.io.nio;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2016/12/15
 * Time: 16:47
 * To change this template use File | Settings | File Templates.
 */
public class NioDemo {


    public void channelToBuffer1() throws Exception{
        //创建随机读写文件 r为只读 rw 为读写模式
        RandomAccessFile randomAccessFile = new RandomAccessFile("C:demo.txt","rw");
        FileChannel fileChannel = randomAccessFile.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(48);//设置大小 字节
        int byteRead= fileChannel.read(byteBuffer);

        while (byteRead != -1) {
            System.out.println("Read:" + byteRead);
            byteBuffer.flip();//读取数据到buff,然后反转buff,从buff读取数据
            while (byteBuffer.hasRemaining()){
                System.out.println((char)byteBuffer.get());
            }
            byteBuffer.clear();
            byteRead = fileChannel.read(byteBuffer);
        }
        randomAccessFile.close();
    }


    /**
     * 选择器的使用
     */
    public void selector1(){

    }

    /**
     * socketChannel
     */
    public void socketChannel1(){

    }



    public static void main(String[] args) throws Exception{
        NioDemo nioDemo = new NioDemo();
        nioDemo.channelToBuffer1();





    }

}
